#include "cubeshader.h"

CubeShader::CubeShader(QObject *parent)
    :BaseShader(parent)
{
    m_file_vert = ":/triangles.vert";
    m_file_frag = ":/triangles.frag";
}

void CubeShader::initializeGL()
{
    BaseShader::initializeGL();

    aPos = m_program->attributeLocation("aPos");
    aNormal = m_program->attributeLocation("aNormal");

    uniform_projection = m_program->uniformLocation("projection");
    uniform_view = m_program->uniformLocation("view");
    uniform_model = m_program->uniformLocation("model");

    uniform_objectColor = m_program->uniformLocation("objectColor");
    uniform_lightColor = m_program->uniformLocation("lightColor");
    uniform_lightPos = m_program->uniformLocation("lightPos");
    uniform_viewPos = m_program->uniformLocation("viewPos");


    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(aPos, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), BUFFER_OFFSET(0));
    glVertexAttribPointer(aNormal, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), BUFFER_OFFSET(3 * sizeof(GLfloat)));

    glEnableVertexAttribArray(aPos);
    glEnableVertexAttribArray(aNormal);
}

void CubeShader::paintGL()
{
    m_program->bind();

    m_program->setUniformValue(uniform_projection, projection);
    m_program->setUniformValue(uniform_view, view);
    m_program->setUniformValue(uniform_model, model);

    m_program->setUniformValue(uniform_objectColor, QVector3D(1.0f, 0.5f, 0.31f));
    m_program->setUniformValue(uniform_lightColor, QVector3D(1.0f, 1.0f, 1.0f));
    m_program->setUniformValue(uniform_lightPos, QVector3D( -1.0f, 1.0f, -1.0f)); // 灯位置
    m_program->setUniformValue(uniform_viewPos, viewPos);

    glDrawArrays(GL_TRIANGLES, 0, 36);

    m_program->release();
}
